;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.SaleOrder", [])
        .controller("SaleOrderCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", "$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService, $stateParams) {
            var btnPermissionCode = {
                saleOrderExport: window.ActionCode.saleOrderExport,
                saleOrderRemoveCache: window.ActionCode.saleOrderRemoveCache
            };
            $scope.yesOrNo = [
                {key: "1", value: "是"},
                {key: "0", value: "否"}
            ];
            $scope.deliveryStatues = [];
            $scope.btnPermission = {
                saleOrderExport: true,
                saleOrderRemoveCache: true
            };
            $scope.markInfo = {
                allChecked: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.outState = [
                {id: 0, name: "未出库"},
                {id: 1, name: "部分出库"},
                {id: 2, name: "已出库"},
                {id: 3, name: "强制结单"}
            ];
            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("dReport", "/report/exportSaleReport")
            };
            $scope.condition = {
                purchaseOrgName: $stateParams.purchaseOrgName || undefined
            };
            if ($stateParams.purchaseOrgName) {
                getDataList()
            }
            $scope.dataList = [];
            $scope.toolbarConfig = {
                showColConfig: false
            };
            $scope.tbConf = {
                scrollWidth: 1210,
                tableCol: [
                    {
                        name: "采购单号",
                        dataField: "purchaseNo",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "销售单号",
                        dataField: "saleNo",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "发货通知单号",
                        dataField: "deliveryNo",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "送货单号",
                        dataField: "sendOrder",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "供方采购订单号",
                        dataField: "supplierPurchaseNo",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "制单时间",
                        dataField: "createDateF",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "客户号",
                        dataField: "dealerCode",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "客户名称",
                        dataField: "purchaseOrgName",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "客户分类",
                        dataField: "className",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "制单人",
                        dataField: "createName",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "出库时间",
                        dataField: "outDateF",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "出库状态",
                        dataField: "statusF",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "出库地点",
                        dataField: "outArea",
                        width: 180,
                        isShow: true
                    },
                    {
                        name: "SAP订单号",
                        dataField: "sapSaleOrder",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "SAP外向发货单",
                        dataField: "sapOutBill",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "零件凭证号",
                        dataField: "materialsBillno",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "价格组",
                        dataField: "priceSchemeName",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "备注",
                        dataField: "note",
                        width: 140,
                        isShow: true
                    },
                    // {
                    //     name: "零件分类",
                    //     dataField: "grade",
                    //     width: 120,
                    //     isShow: true
                    // }, add by david
                    {
                        name: "零件分类",
                        dataField: "sortName",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "发运交接单号",
                        dataField: "deliveryBillNo",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "承运商",
                        dataField: "carrierOrgName",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "零件号",
                        dataField: "materialsNo",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "零件中文名",
                        dataField: "materialsDes",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "调整后数量",
                        dataField: "amountAfterAdjust",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "单位",
                        dataField: "unit",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "原单价",
                        dataField: "price",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "原总价",
                        dataField: "applyTotalPrice",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "调整后单价",
                        dataField: "unitPriceAfterAdjust",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "调整后总价",
                        dataField: "totalPriceAfterAdjust",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "本次发货数量",
                        dataField: "deliveryQty",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "中心仓库数量",
                        dataField: "centralWarehouseAmount",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "实际出库数量",
                        dataField: "outAmount",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "出库金额",
                        dataField: "outPrice",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "是否开票",
                        dataField: "isInvoiceF",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "成本价",
                        dataField: "standardPrice",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "订单金额",
                        dataField: "orderTotal",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "发货日期",
                        dataField: "deliveryDateF",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "SAP是否同步",
                        dataField: "sapUpdateF",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "到货日期",
                        dataField: "realArrivalDateF",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "签收日期",
                        dataField: "receiveDateF",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "签收状态",
                        dataField: "deliveryStatus",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "开票时间",
                        dataField: "invoiceDateF",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "服务信息单号",
                        dataField: "crmServiceNo",
                        width: 140,
                        isShow: true
                    }
                ]
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.selectSingle = function (item, list, type) {
                var selectAll = false;
                if (!item.isChecked) {
                    selectAll = false;
                } else {
                    if (!checkAll(list)) {
                        selectAll = false;
                    } else {
                        selectAll = true;
                    }
                }
                if (angular.isDefined(type)) {
                    $scope.markInfo["allChecked" + type] = selectAll;
                } else {
                    $scope.markInfo.allChecked = selectAll;
                }
            };

            $scope.selectAll = function (list, type) {
                for (var i = 0, len = list.length; i < len; i++) {
                    list[i].isChecked = angular.isUndefined(type) ? $scope.markInfo.allChecked : $scope.markInfo["allChecked" + type];
                }
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                $scope.markInfo.curRow = -1;
                var service = "dReport";
                var reqUrl = "/report/saleReportList";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    var df = "yyyy-MM-dd HH:mm:ss";
                    angular.forEach(resp.value.list, function (item) {
                        item.createDateF = $filter("YbDate")(item.createDate, df);
                        item.outDateF = $filter("YbDate")(item.outDate, df);
                        item.deliveryDateF = $filter("YbDate")(item.deliveryDate, df);
                        item.realArrivalDateF = $filter("YbDate")(item.realArrivalDate, "yyyy-MM-dd");
                        item.receiveDateF = $filter("YbDate")(item.receiveDate, df);
                        item.isInvoiceF = yesOrNo(item.isInvoice);
                        item.sapUpdateF = yesOrNo(item.sapUpdate);
                        item.statusF = $filter("ProgressStatus")(item.status, "outDepot");
                        item.isChecked = false;
                        item.invoiceDateF = $filter("YbDate")(item.invoiceDate, df);
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            function yesOrNo(val) {
                if (String(val) === "1") {
                    return "是"
                } else if (String(val) === "0") {
                    return "否"
                } else {
                    return val;
                }
            }

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            function getDeliveryStatus() {
                var service = "device";
                var reqUrl = "/dataDictionary/searchList";
                var param = {parentCode: "WLZT"};
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.deliveryStatues = resp.value;
                })
            }

            $scope.removeCache = function () {
                var service = "device";
                var reqUrl = "/cache/remove/SALE_ORDER";

                ybWidget.ShowMessage("确定要清除导出缓存吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Post(service, reqUrl).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                    });
                });
            };

            (function () {
                var formMaterialStorageParam = JSON.parse(window.sessionStorage.getItem("saleOrderPageParam"));
                window.sessionStorage.removeItem("saleOrderPageParam");
                if (!!formMaterialStorageParam) {
                    $scope.condition.saleNo = formMaterialStorageParam.billNo;
                    $scope.condition.materialsNo = formMaterialStorageParam.materialsNo;
                    getDataList()
                } else {
                    //getDataList();
                }

                getDeliveryStatus();
                UserContextService.SetBtnPermission(setBtnPermission);

                if ($scope.isWJ) {
                    $scope.tbConf.tableCol = ybWidget.HideTableCols($scope.tbConf.tableCol, ['standardPrice']);
                }
            })();
        }]);
    ;
})(window, window.angular);